home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / MySql / scripts / mysqldumpslow < prev    next >
Encoding:
Text File  |  2001-01-01  |  4.7 KB  |  139 lines

  1. #!/bin/perl
  2. # mysqldumpslow - parse and summarize the MySQL slow query log
  3.  
  4. # Original version by Tim Bunce, sometime in 2000.
  5. # Further changes by Tim Bunce, 8th March 2001.
  6.  
  7. use strict;
  8. use Getopt::Long;
  9.  
  10. # t=time, l=lock time, r=rows
  11. # at, al, and ar are the corresponding averages
  12.  
  13. my %opt = (
  14.     s => 'at',
  15.     h => '*',
  16. );
  17.  
  18. GetOptions(\%opt,
  19.     'v+',    # verbose
  20.     'd+',    # debug
  21.     's=s',    # what to sort by (t, at, l, al, r, ar etc)
  22.     'r!',    # reverse the sort order (largest last instead of first)
  23.     't=i',    # just show the top n queries
  24.     'a!',    # don't abstract all numbers to N and strings to 'S'
  25.     'n=i',    # abstract numbers with at least n digits within names
  26.     'g=s',    # grep: only consider stmts that include this string
  27.     'h=s',    # hostname of db server for *-slow.log filename (can be wildcard)
  28.     'i=s',    # name of server instance (if using mysql.server startup script)
  29.     'l!',    # don't subtract lock time from total time
  30. ) or die "Bad option";
  31.  
  32.  
  33. unless (@ARGV) {
  34.     my $defaults   = `my_print_defaults mysqld`;
  35.     my $basedir = ($defaults =~ m/--basedir=(.*)/)[0]
  36.     or die "Can't determine basedir from 'my_print_defaults mysqld' output: $defaults";
  37.     warn "basedir=$basedir\n" if $opt{v};
  38.  
  39.     my $datadir = ($defaults =~ m/--datadir=(.*)/)[0];
  40.     if (!$datadir or $opt{i}) {
  41.     # determine the datadir from the instances section of /etc/my.cnf, if any
  42.     my $instances  = `my_print_defaults instances`;
  43.     die "Can't determine datadir from 'my_print_defaults mysqld' output: $defaults"
  44.         unless $instances;
  45.     my @instances = ($instances =~ m/^--(\w+)-/mg);
  46.     die "No -i 'instance_name' specified to select among known instances: @instances.\n"
  47.         unless $opt{i};
  48.     die "Instance '$opt{i}' is unknown (known instances: @instances)\n"
  49.         unless grep { $_ eq $opt{i} } @instances;
  50.     $datadir = ($instances =~ m/--$opt{i}-datadir=(.*)/)[0]
  51.         or die "Can't determine --$opt{i}-datadir from 'my_print_defaults instances' output: $instances";
  52.     warn "datadir=$datadir\n" if $opt{v};
  53.     }
  54.  
  55.     @ARGV = <$datadir/$opt{h}-slow.log>;
  56.     die "Can't find '$datadir/$opt{h}-slow.log'\n" unless @ARGV;
  57. }
  58.  
  59. warn "\nReading mysql slow query log from @ARGV\n";
  60.  
  61. my @pending;
  62. my %stmt;
  63. $/ = ";\n#";        # read entire statements using paragraph mode
  64. while ( defined($_ = shift @pending) or defined($_ = <>) ) {
  65.     warn "[[$_]]\n" if $opt{d};    # show raw paragraph being read
  66.  
  67.     my @chunks = split /^\/.*Version.*started with[\000-\377]*?Time.*Id.*Command.*Argument.*\n/m;
  68.     if (@chunks > 1) {
  69.     unshift @pending, map { length($_) ? $_ : () } @chunks;
  70.     warn "<<".join(">>\n<<",@chunks).">>" if $opt{d};
  71.     next;
  72.     }
  73.  
  74.     s/^#? Time: \d{6}\s+\d+:\d+:\d+.*\n//;
  75.     my ($user,$host) = s/^#? User\@Host:\s+(\S+)\s+\@\s+(\S+).*\n// ? ($1,$2) : ('','');
  76.  
  77.     s/^# Time: (\d+)  Lock_time: (\d+)  Rows_sent: (\d+).*\n//;
  78.     my ($t, $l, $r) = ($1, $2, $3);
  79.     $t -= $l unless $opt{l};
  80.  
  81.     # remove fluff that mysqld writes to log when it (re)starts:
  82.     s!^/.*Version.*started with:.*\n!!mg;
  83.     s!^Tcp port: \d+  Unix socket: \S+\n!!mg;
  84.     s!^Time.*Id.*Command.*Argument.*\n!!mg;
  85.  
  86.     s/^use \w+;\n//;    # not consistently added
  87.     s/^SET timestamp=\d+;\n//;
  88.  
  89.     s/^[     ]*\n//mg;    # delete blank lines
  90.     s/^[     ]*/  /mg;    # normalize leading whitespace
  91.     s/\s*;\s*(#\s*)?$//;    # remove trailing semicolon(+newline-hash)
  92.  
  93.     next if $opt{g} and !m/$opt{g}/io;
  94.  
  95.     unless ($opt{a}) {
  96.     s/\b\d+\b/N/g;
  97.     s/\b0x[0-9A-Fa-f]+\b/N/g;
  98.     s/'.*?'/'S'/g;
  99.     s/".*?"/"S"/g;
  100.     # -n=8: turn log_20001231 into log_NNNNNNNN
  101.     s/([a-z_]+)(\d{$opt{n},})/$1.('N' x length($2))/ieg if $opt{n};
  102.     # abbreviate massive "in (...)" statements and similar
  103.     s!(([NS],){100,})!sprintf("$2,{repeated %d times}",length($1)/2)!eg;
  104.     }
  105.  
  106.     my $s = $stmt{$_} ||= { users=>{}, hosts=>{} };
  107.     $s->{c} += 1;
  108.     $s->{t} += $t;
  109.     $s->{l} += $l;
  110.     $s->{r} += $r;
  111.     $s->{users}->{$user}++ if $user;
  112.     $s->{hosts}->{$host}++ if $host;
  113.  
  114.     warn "{{$_}}\n\n" if $opt{d};    # show processed statement string
  115. }
  116.  
  117. foreach (keys %stmt) {
  118.     my $v = $stmt{$_} || die;
  119.     my ($c, $t, $l, $r) = @{ $v }{qw(c t l r)};
  120.     $v->{at} = $t / $c;
  121.     $v->{al} = $l / $c;
  122.     $v->{ar} = $r / $c;
  123. }
  124.  
  125. my @sorted = sort { $stmt{$b}->{$opt{s}} <=> $stmt{$a}->{$opt{s}} } keys %stmt;
  126. @sorted = @sorted[0 .. $opt{t}-1] if $opt{t};
  127. @sorted = reverse @sorted         if $opt{r};
  128.  
  129. foreach (@sorted) {
  130.     my $v = $stmt{$_} || die;
  131.     my ($c, $t,$at, $l,$al, $r,$ar) = @{ $v }{qw(c t at l al r ar)};
  132.     my @users = keys %{$v->{users}};
  133.     my $user  = (@users==1) ? $users[0] : sprintf "%dusers",scalar @users;
  134.     my @hosts = keys %{$v->{hosts}};
  135.     my $host  = (@hosts==1) ? $hosts[0] : sprintf "%dhosts",scalar @hosts;
  136.     printf "Count: %d  Time=%.2fs (%ds)  Lock=%.2fs (%ds)  Rows=%.1f (%d), $user\@$host\n%s\n\n",
  137.         $c, $at,$t, $al,$l, $ar,$r, $_;
  138. }
  139.